/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Calendar
{
	/// <summary>
	/// Represents an entry in a Groove calendar tool.
	/// </summary>
	public class CalendarEntry : GrooveWebServicesV12HelpersBase
	{
		private GrooveWebServicesV12Helpers.Calendar.CalendarTool m_CalendarTool;
		
		private GrooveCalendarWebService.CalendarEntry m_CalendarEntry;

		public CalendarEntry(GrooveWebServicesV12Helpers.Calendar.CalendarTool i_CalendarTool, GrooveCalendarWebService.CalendarEntry i_CalendarEntry)
			: base("", i_CalendarEntry.URI, i_CalendarTool.IdentityURL, i_CalendarTool.Context)
		{
			m_CalendarTool = i_CalendarTool;
			m_CalendarEntry = i_CalendarEntry;
		}

		#region Calendar Properties And Methods

		/// <summary>
		/// The CalendarTool which contains this CalendarEntry.
		/// </summary>
		public GrooveWebServicesV12Helpers.Calendar.CalendarTool CalendarTool
		{
			get
			{
				return m_CalendarTool;
			}
		}

		/// <summary>
		/// True if this is an all-day event.
		/// </summary>
		public bool AllDay
		{
			get
			{
				return m_CalendarEntry.AllDay;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.AllDay == value)
					return;

				bool OldValue = m_CalendarEntry.AllDay;

				try
				{
					//  Update local instance
					m_CalendarEntry.AllDay = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.AllDay = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The category of this entry.
		/// </summary>
		public string Category
		{
			get
			{
				return m_CalendarEntry.Category;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.Category == value)
					return;

				string OldValue = m_CalendarEntry.Category;

				try
				{
					//  Update local instance
					m_CalendarEntry.Category = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.Category = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The DateTime this entry was created.
		/// </summary>
		public System.DateTime Created
		{
			get
			{
				return m_CalendarEntry.Created;
			}
		}

		/// <summary>
		/// IdentityURL of the Groove user who created this CalendarEntry
		/// </summary>
		public string CreatedBy
		{
			get
			{
				return m_CalendarEntry.CreatedBy;
			}
		}

		/// <summary>
		/// The description of this CalendarEntry.
		/// </summary>
		public string Description
		{
			get
			{
				return m_CalendarEntry.Description;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.Description == value)
					return;

				string OldValue = m_CalendarEntry.Description;

				try
				{
					//  Update local instance
					m_CalendarEntry.Description = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.Description = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The details of this CalendarEntry.
		/// </summary>
		public string Details
		{
			get
			{
				return m_CalendarEntry.Details;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.Details == value)
					return;

				string OldValue = m_CalendarEntry.Details;

				try
				{
					//  Update local instance
					m_CalendarEntry.Details = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.Details = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The end-datetime of this CalendarEntry.
		/// </summary>
		public System.DateTime End
		{
			get
			{
				return m_CalendarEntry.End;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.End == value)
					return;

				System.DateTime OldValue = m_CalendarEntry.End;

				try
				{
					//  Update local instance
					m_CalendarEntry.End = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.End = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The ID of this CalendarEntry.
		/// </summary>
		public string ID
		{
			get
			{
				return m_CalendarEntry.ID;
			}
		}

		/// <summary>
		/// The DateTime this CalendarEntry was last modified.
		/// </summary>
		public System.DateTime Modified
		{
			get
			{
				return m_CalendarEntry.Modified;
			}
		}

		/// <summary>
		/// IdentityURL of the Groove user who last modified this CalendarEntry.
		/// </summary>
		public string ModifiedBy
		{
			get
			{
				return m_CalendarEntry.ModifiedBy;
			}
		}

		/// <summary>
		/// The start-DateTime of this CalendarEntry.
		/// </summary>
		public System.DateTime Start
		{
			get
			{
				return m_CalendarEntry.Start;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_CalendarEntry.Start == value)
					return;

				System.DateTime OldValue = m_CalendarEntry.Start;

				try
				{
					//  Update local instance
					m_CalendarEntry.Start = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_CalendarEntry.Start = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// True if this CalendarEntry is unread.
		/// </summary>
		public bool Unread
		{
			get
			{
				return m_CalendarEntry.Unread;
			}
		}

		/// <summary>
		/// Marks this CalendarEntry as read.
		/// </summary>
		public void MarkRead()
		{
			//  Get initialized reference to web service
			GrooveCalendarWebService.GrooveCalendar CalendarWebService = GrooveWebServicesV12Helpers.Calendar.CalendarTool.CreateInitializedCalendarWebService
				(
				m_IdentityURL,
				m_CalendarEntry.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to delete calendar entry
			CalendarWebService.UpdateUnreadMark(false);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("CalendarWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		/// <summary>
		/// Marks this CalendarEntry as unread.
		/// </summary>
		public void MarkUnread()
		{
			//  Get initialized reference to web service
			GrooveCalendarWebService.GrooveCalendar CalendarWebService = GrooveWebServicesV12Helpers.Calendar.CalendarTool.CreateInitializedCalendarWebService
				(
				m_IdentityURL,
				m_CalendarEntry.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to delete calendar entry
			CalendarWebService.UpdateUnreadMark(true);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("CalendarWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		/// <summary>
		/// Updates multiple properties of this CalendarEntry.
		/// </summary>
		/// <param name="i_Category">Category</param>
		/// <param name="i_Description">Description</param>
		/// <param name="i_Details">Details</param>
		/// <param name="i_AllDay">true if this is an all-day entry</param>
		/// <param name="i_Start">Start datetime</param>
		/// <param name="i_End">End datetime</param>
		public void Update(string i_Category, string i_Description, string i_Details, bool i_AllDay, System.DateTime i_Start, System.DateTime i_End)
		{
			string Category_OldValue = i_Category;
			string Description_OldValue = i_Description;
			string Details_OldValue = i_Details;

			bool AllDay_OldValue = i_AllDay;
			
			System.DateTime Start_OldValue = i_Start;
			System.DateTime End_OldValue = i_End;

			try
			{
				//  Update local instance
				m_CalendarEntry.AllDay = i_AllDay;
				m_CalendarEntry.Category = i_Category;
				m_CalendarEntry.Description = i_Description;
				m_CalendarEntry.Details = i_Details;
				m_CalendarEntry.End = i_End;
				m_CalendarEntry.Start = i_Start;

				//  Update data
				int_Update();
			}
			catch (System.Exception Exception)
			{
				//  Revert local instance
				m_CalendarEntry.AllDay = AllDay_OldValue;
				m_CalendarEntry.Category = Category_OldValue;
				m_CalendarEntry.Description = Description_OldValue;
				m_CalendarEntry.Details = Details_OldValue;
				m_CalendarEntry.End = End_OldValue;
				m_CalendarEntry.Start = Start_OldValue;

				throw Exception;
			}
		}

		private void int_Update()
		{
			//  Get initialized reference to web service
			GrooveCalendarWebService.GrooveCalendar CalendarWebService = GrooveWebServicesV12Helpers.Calendar.CalendarTool.CreateInitializedCalendarWebService
				(
				m_IdentityURL,
				m_CalendarEntry.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to update calendar entry
			CalendarWebService.UpdateEntry(m_CalendarEntry);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("CalendarWebService::UpdateEntry, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		#endregion

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
					m_BindableURL = m_CalendarTool.BindableURL + "/" + this.ID;			

				return m_BindableURL;
			}
		}

		#endregion
	}
}
